﻿using Cinemachine;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Fxb.CinemachineEx
{
    public class CameraOffsetExtension : CinemachineExtension
    {
        [Tooltip("控制摄像机前后移动。 单位米")]
        [AxisStateProperty]
        public AxisState offsetAxis = new AxisState(-1.5f, 1.5f, false, false, 50f, 0.1f, 0.1f, "Mouse ScrollWheel", true);

        [AxisStateProperty]
        public AxisState touchAxis = new AxisState(-1.5f, 1.5f, false, false, 50f, 0.1f, 0.1f, "Multi Finger", true);

        public Vector3 Offset { get; protected set; }

        public override bool OnTransitionFromCamera(ICinemachineCamera fromCam, Vector3 worldUp, float deltaTime)
        {
            offsetAxis.Reset();

            offsetAxis.Value = 0;

            return base.OnTransitionFromCamera(fromCam, worldUp, deltaTime);
        }

        protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
        {
            //Aim阶段 && 位置信息未被其他扩展脚本修改(修改为CinemachineCollider时不再运行)
            if (stage != CinemachineCore.Stage.Aim || state.PositionCorrection != Vector3.zero)
                return;

            if (deltaTime > 0 && CinemachineCore.Instance.IsLive(vcam))
            {
#if TOUCHMODEL
                touchAxis.Update(deltaTime);
                Offset = state.RawOrientation * (Vector3.forward * -touchAxis.Value);
#else
                offsetAxis.Update(deltaTime);
                //目前逻辑为单纯向前  可以考虑改成直接向lookat目标位置移动
                Offset = state.RawOrientation * (Vector3.forward * -offsetAxis.Value);
#endif
                state.PositionCorrection += Offset;
            }
        }
    }
}
